/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_TOOLS_TYPES_RELATION_NAMELIST_HPP
#define	_CUBEFS_CORE_TOOLS_TYPES_RELATION_NAMELIST_HPP

#include <cubefs/core/tools/types/relation/expression.hpp>
#include <cubefs/core/tools/types/algo.hpp>
#include <cubefs/core/tools/types/typelist.hpp>
#include <cubefs/core/tools/assert.hpp>

namespace cubefs    {
namespace core      {
namespace tools     {
namespace types     {
namespace relation  {

using namespace cubefs::core::tools::types::algo;
using namespace cubefs::core::tools::types::typelist;

template <typename Item, typename List>
class AddIfNotContains {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);

public:
    static const Int Idx = IndexOfTool<Item, List>::Value;
public:
    typedef typename IfThenElse<Idx == -1, TypeList<Item, List>, List>::Result Result;
};


template <typename Expr, typename List = NullType> class NameListBuilder;

template <typename Type1, typename Type2, typename List>
class NameListBuilder<And<Type1, Type2>, List> {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);

    typedef typename NameListBuilder<Type1, List>::Result                       Result1;
    typedef typename NameListBuilder<Type2, Result1>::Result                    Result2;
public:
    typedef Result2                                                             Result;
};


template <typename Type1, typename Type2, typename List>
class NameListBuilder<Or<Type1, Type2>, List> {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);

    typedef typename NameListBuilder<Type1, List>::Result                       Result1;
    typedef typename NameListBuilder<Type2, Result1>::Result                    Result2;
public:
    typedef Result2                                                             Result;
};

template <typename Type1, typename Type2, typename List>
class NameListBuilder<Xor<Type1, Type2>, List> {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);

    typedef typename NameListBuilder<Type1, List>::Result                       Result1;
    typedef typename NameListBuilder<Type2, Result1>::Result                    Result2;
public:
    typedef Result2                                                             Result;
};

template <typename Type, typename List>
class NameListBuilder<Not<Type>, List> {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);
public:
    typedef typename NameListBuilder<Type, List>::Result                        Result;
};


template <Int Name, CompareOps Op, typename Type, Type ExValue, typename List>
class NameListBuilder<ValueOp<Name, Op, Type, ExValue>, List> {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);
public:
    typedef typename AddIfNotContains<Name, List>::Result                       Result;
};

template <Int Name, typename Type, typename List>
class NameListBuilder<TypeOp<Name, Type>, List> {
    CUBEFS_STATIC_ASSERT(IsList<List>::Value);
public:
    typedef typename AddIfNotContains<Name, List>::Result                       Result;
};


}
}
}
}
}

#endif //_CUBEFS_CORE_TOOLS_TYPES_RELATION_EXPRESSION_HPP